import 'package:flutter/material.dart';
import '../client_dio.dart';
import 'dart:convert';
class TableRoute extends StatefulWidget {
  @override
  State createState() => new TableRouteState();
}
class TableRouteState extends State<TableRoute> {
  var num1 = 0, num2 = 0, sum = 0;
  var rows=[TableRow(
              decoration: BoxDecoration(color: Colors.grey[200]),
              children: 
                [
                  Text("用户"),
                  Text("仪器型号"),
                  Text("仪器编号"),
                ]
              )];
  final TextEditingController t1 = new TextEditingController(text: "0");
  final TextEditingController t2 = new TextEditingController(text: "0");
  void login_click(){
    login("马红权", "333333", (r) {
      items({"limit":10}, (r) {
        var res=json.decode(r);
        print(res);
        var newRows=<TableRow>[];
        newRows.add(TableRow(
              decoration: BoxDecoration(color: Colors.grey[200]),
              children: 
                [
                  Text("id"),
                  Text("bh"),
                  Text("name"),
                  Text("op"),
                ]
              ));
        for(var i=0;i<res["data"].length;i++){
          newRows.add(TableRow(
              children: ([
                Text(res["data"][i]["id"].toString()),
                Text(res["data"][i]["bh"]==null?"":res["data"][i]["bh"]),
                Text(res["data"][i]["name"]==null?"":res["data"][i]["name"]),
                new MaterialButton(
                  child: new Text("edit"),
                  color: Colors.greenAccent,
                  onPressed: (){doEdit(i);},
                )
              ]),
            ));
        }
        setState((){
          rows=newRows;
        });
      }, err_treat);
    }, (e) {
      print(e);
    });
  }
  void doEdit(i){
    print("doEdit");
    print(i);
  }
  void doClear() {
    setState(() {
      t1.text = "0";
      t2.text = "0";
    });
  }
  _addPadding(List<Widget> w) {
    return w
        .map((e) => Padding(
              padding: EdgeInsets.all(6),
              child: e,
            ))
        .toList();
  }
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Container(
        padding: const EdgeInsets.all(40.0),
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
          ElevatedButton(
          child: Text("login"),
          onPressed: login_click
        ),
        Table(
          border: TableBorder.all(),
          defaultVerticalAlignment: TableCellVerticalAlignment.middle,
          children: rows,
        ),
            new Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                new MaterialButton(
                  child: new Text("Clear"),
                  color: Colors.greenAccent,
                  onPressed: doClear,
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}

